package io.acm.common.utils;

import io.acm.modules.app.entity.StuFileEntity;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelParser {

    public static List<StuFileEntity> parseUsersFromExcel(InputStream inputStream) throws IOException {
        List<StuFileEntity> users = new ArrayList<>();

        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0); // Assuming data is in the first sheet

        for (Row row : sheet) {
            StuFileEntity user = new StuFileEntity();
            //姓名 学号 性别 学院 专业 班级
            user.setUserName(row.getCell(0).getStringCellValue());
            user.setSId((row.getCell(1).getStringCellValue()));
            //user.setSId((String.valueOf(row.getCell(1).getNumericCellValue())));
            String value = row.getCell(2).getStringCellValue();
            if(value.equals("男")) user.setSex(1);
            else user.setSex(0);
            user.setCollege(row.getCell(3).getStringCellValue());
            user.setMajor(row.getCell(4).getStringCellValue());
            user.setClassName(row.getCell(5).getStringCellValue());
            //user.setPhone(row.getCell(4).getStringCellValue());
            //user.setPhone((String.valueOf(row.getCell(4).getNumericCellValue())));
            //user.setEmail(row.getCell(5).getStringCellValue());

            users.add(user);
        }

        workbook.close();

        return users;
    }
}